;(function($){
	$.fn.many_unit = function(opt){
		var el = this;
		var opts = {
			ds : {},
			dw2 : "",
			dwtype : "",
			dataBack : function(){},
			closeBack : function(){},
		}
		var param = $.extend(opts,opt);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHtml();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			
			//确定
			el.find("#modalBtnCheck").click(function(){
				var dw = el.find("#manyUnitForm").find("#dw").val();
				var dw2 = el.find("#manyUnitForm").find("#dw2").val();
				param.ds.dw2ST = true;
				param.ds.sl = t.checkTwoPoint(dw);
				if(dw2){
					param.ds.zf = t.checkTwoPoint(dw2) + param.ds.dw2;
				}
				$("#mykeyboard").css("display","none");
				param.dataBack(param.ds);
				el.empty();
				el.modal("hide");
			});
			
			//关闭
			el.find("#close-dialog,#modalBtnBack").click(function(){
				$("#mykeyboard").css("display","none");
				el.empty();
				el.modal("hide");
				param.ds.dw2ST = true;
				param.closeBack();
			})
			
			//多单位点击事件
			el.find("#dw2_help,#dw_help").click(function(){//.find("input").
				
				var top =$(this).offset().top - 10;//是标签距离顶部高度（没有到下面的距离，比如$("div").offset().down）
				var left = $(this).offset().left - 110;//是标签距离右边高度（没有到下面的距离，比如$("div").offset().right）
				var number = $(this).val();
				var id = $(this).attr("id");
				var title = $.i18n.propJsp("数量");
				var name = $(this).data("name");
				var $this = el.find("#"+name);
				if(!$.isFunction($().digitalCalculator)){
					$.ajaxSetup({ async: false, cache: true });
		            $.getScript("js/openSpa/jquery.plugin.digitalCalculator.js");
		            $.ajaxSetup({ async: true , cache: false});
				}
				$("#mykeyboard").digitalCalculator({
					number:number,//数量
					status:status?status:true,
					type : "price",//pNum:人数;discount:小数折扣;percent:百分比;price:两位小数;
					//初始化数据返回回调
					dataBack:function(data){
						$this.val(data)
//						el.find("#manyUnitForm").find("#"+id).val(data);
//						if(id=="dw2"){
//							param.dwtype = "dw2";
//							el.find("#manyUnitForm").find("#dw").val("");
//						}else{
//							param.dwtype = "dw";
//							el.find("#manyUnitForm").find("#dw2").val("");
//						}
					},
					returnBack : function(){
						
					}
				});
			})
			
			//多单位点击事件
			el.find("#manyUnitForm").find("input").keyup(function(){
				var id = $(this).attr("id");
				if(id=="dw2"){
					param.dwtype = "dw2";
//					el.find("#manyUnitForm").find("#dw").val("");
				}else{
					param.dwtype = "dw";
//					el.find("#manyUnitForm").find("#dw2").val("");
				}
			});
			
			//数量、单位可修改
    		el.find(".inputEntry").click(function(){
    			$(this).select();
    		})
    		
			el.find(".inputEntry").keyup(function(){
    			var name = $(this).attr("name");
    			var $dom=el.find("input[name="+name+"]");
    			var value = $(this).val().replace(/[^\-^\d^\.]+/g,"");
    			$(this).val(value);
    		})
		},
		initHtml : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
			var zf = "";
			if(param.ds.zf){
				zf = param.ds.zf.replace(param.ds.dw2,"");
			}
			el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
			var html=	'<div id="mainModal" class="modal-dialog" style="width:400px;">'+
							'<div id="pageOverlay" class="hide"></div>'+
								'<div id="loading" class="loading hide">'+
								'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
							'</div>'+
							'<div class="modal-content">'+
					  			'<div class="modal-header no-padding">'+ 
									'<div class="table-header">'+
										'<button id="close-dialog" type="button" class="close" data-dismiss="modal" aria-hidden="true" value="HTML" style="zoom:140%;">'+
											'<span class="white">&times;</span>'+
										'</button>'+
										'<span id="modal-h4" style="zoom:120%;font-weight:bold;">'+$.i18n.propJsp("多单位")+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="widget-body">'+
									
									'<div class="space-6"></div>'+
									'<form id="manyUnitForm" class="form-horizontal" role="form">'+
										
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label no-padding-right" for="form-field-4" style="font-size:22px;">'+param.ds.dw+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group">'+
													'<input class="input-lg inputEntry" type="text" id="dw" name="dw" value="'+param.ds.sl+'"/>'+//input-xxlarge   
													'<span class="input-group-btn" style="margin-left:0px;">'+
														'<button type="button" data-name="dw" class="btn btn-primary btn-sm" id="dw_help" style="height:46px;width:80px;">'+
															'<span class="ace-icon fa fa-keyboard-o icon-on-right bigger-200"></span>'+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										
										'<div class="form-group">'+
											'<label class="col-sm-3 control-label no-padding-right" for="form-field-4" style="font-size:22px;">'+(param.dw2||param.ds.dw2)+'</label>'+
											'<div class="col-sm-9">'+
												'<div class="input-group">'+
													'<input class="input-lg inputEntry" type="text" id="dw2" name="dw2" value=""/>'+//input-xxlarge 
													'<span class="input-group-btn" style="margin-left:0px;">'+
														'<button type="button" data-name="dw2" class="btn btn-primary btn-sm" id="dw2_help" style="height:46px;width:80px;">'+
															'<span class="ace-icon fa fa-keyboard-o icon-on-right bigger-200"></span>'+
														'</button>'+
													'</span>'+
												'</div>'+
											'</div>'+
										'</div>'+
										
									'</form>'+
								
								'</div>'+				
								'<div class="modal-footer">'+
									'<div class="clearfix">'+	
										'<div class="pull-right">'+
											'<button id="modalBtnCheck" class="btn btn-info btn-sm">'+
												'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("确定")+
											'</button>'+
											'<button id="modalBtnBack" class="btn btn-default btn-sm">'+
												'<i class="ace-icon fa fa-undo"></i>'+$.i18n.propJsp("返回")+
											'</button>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>';
			el.html(html);
		},
		//俩位小数校验
    	checkTwoPoint:function(value){
    		var $t=this;
    		var $opt=$t.options;
    		var el=$t.$element;
    		
    		value=value.toString()+".00";
//    		value = value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
//    		value = value.replace(/^\./g,""); //验证第一个字符是数字
    		value = value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
    		value = value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
    		value = value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
    		
    		return value;
    	},
	}
	
})(jQuery);